import { _decorator, Component, Node, Animation } from 'cc';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('AnimationController')
@requireComponent(Animation)
export class AnimationController extends Component {
    animation: Animation = null!;
    animName = '';

    protected onLoad(): void {
        this.animation = this.getComponent(Animation);

        this.animation.on(Animation.EventType.PLAY, (...anim) => {});

        this.animation.on(
            Animation.EventType.LASTFRAME,
            (anim: Animation) => {},
        );

        this.animation.on(
            Animation.EventType.FINISHED,
            (anim: Animation) => {},
        );



        // this.scheduleOnce(() => {
        //     this.play('idle');
        // }, 2);
        // this.scheduleOnce(() => {
        //     this.play('run');
        // }, 4);
        // this.scheduleOnce(() => {
        //     this.play('idle');
        // }, 10);
    }

    play(animName: string, isLoop = true) {
        if (this.animName == animName) {
            return;
        }
        this.animName = animName;
        this.animation.play(animName);
    }

}
